package com.hjqjl.whapplib.utils.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.hjqjl.whapplib.R;

/**
 * 对话框便捷设置工具类
 */
public class DialogUtil {
    private static DialogUtil dialogUtil;

    /**
     * 定制dialog，在一个项目中好几个相同的dialog时使用
     */
    public static AlertDialog showMyDialog(Context context, String content, String ok, String cancel, OnClickListener onOKClickListener, OnClickListener onCancelClickListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(context, R.layout.dialog_custom, null);
        dialog.setView(view);
        dialog.show();

        TextView hintTv = view.findViewById(R.id.tv_dialog_content);
        TextView confirmTv = view.findViewById(R.id.tv_dialog_ok);
        TextView cancelTv = view.findViewById(R.id.tv_dialog_cancel);
        hintTv.setText(content);
        confirmTv.setText(ok);
        cancelTv.setText(cancel);
        confirmTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onOKClickListener != null) {
                    onOKClickListener.onClick(dialog);
                }
            }
        });
        cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCancelClickListener != null) {
                    onCancelClickListener.onClick(dialog);
                }
            }
        });
        return dialog;
    }

    /**
     * 对话框按钮点击事件监听器
     */
    public interface OnClickListener {
        /**
         * 点击确定或取消按钮回调方法
         */
        void onClick(AlertDialog dialog);
    }

    /* -----------------------流式API调用以下方法定制对话框样式----------------------- */

    private static AlertDialog dialog;
    private static Window window;

    /**
     * 应最先调用，开始对话框样式的定制
     *
     * @param dialog AlertDialog
     * @return 当前类对象DialogUtil
     */
    public static DialogUtil setting(AlertDialog dialog) {
        if (dialog != null) {
            DialogUtil.dialog = dialog;
            window = dialog.getWindow();
        }
        if (dialogUtil == null) {
            dialogUtil = new DialogUtil();
        }
        return dialogUtil;
    }

    /**
     * 设置宽高
     *
     * @param width  宽
     * @param height 高
     * @return 当前类对象DialogUtil
     */
    public DialogUtil setLayout(int width, int height) {
        if (window != null) {
            window.setLayout(width, height);
        }
        return this;
    }

    /**
     * 设置Gravity
     *
     * @param gravity gravity
     * @return 当前类对象DialogUtil
     */
    public DialogUtil setGravity(int gravity) {
        if (window != null) {
            window.setGravity(gravity);
        }
        return this;
    }

    /**
     * 设置Gravity
     *
     * @param gravity gravity
     * @param xoff    x方向的偏移量
     * @param yoff    y方向的偏移量
     * @return 当前类对象DialogUtil
     */
    public DialogUtil setGravity(int gravity, int xoff, int yoff) {
        if (window != null) {
            window.setGravity(gravity);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.x = xoff;
            lp.y = yoff;
            window.setAttributes(lp);
        }
        return this;
    }

    /**
     * 设置对话框弹出关闭动画，可以在style定义，属性为windowEnterAnimation和windowExitAnimation
     *
     * @param resId 资源id
     * @return 当前类对象DialogUtil
     */
    public DialogUtil setWindowAnimations(int resId) {
        if (window != null) {
            window.setWindowAnimations(resId);
        }
        return this;
    }

    /**
     * 设置对话框弹出时，后面的页面透明度
     *
     * @param activity Activity
     * @param alpha    透明度0-1
     * @return 当前类对象DialogUtil
     */
    public DialogUtil setBgAlpha(Activity activity, float alpha) {
        if (activity != null) {
            if (alpha > 1 || alpha < 0) {
                return this;
            }
            final Window activityWindow = activity.getWindow();
            WindowManager.LayoutParams lp = activityWindow.getAttributes();
            lp.alpha = alpha;
            activityWindow.setAttributes(lp);

            dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    WindowManager.LayoutParams lp = activityWindow.getAttributes();
                    lp.alpha = 1;
                    activityWindow.setAttributes(lp);

                }
            });
        }
        return this;
    }

    /*------------------------------other--------------------------------------*/

    /**
     * 设置对话框底部弹出模式
     *
     * @param dialog   AlertDialog
     * @param activity Activity
     * @param alpha    透明度0-1
     */
    public void setBottomMode(AlertDialog dialog, Activity activity, float alpha) {
        if (dialog == null) {
            return;
        }
        Window window = dialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.BOTTOM);
            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            window.setWindowAnimations(R.style.BottomDialogAnimation);
        }

        if (activity == null || alpha > 1 || alpha < 0) {
            return;
        }

        final Window activityWindow = activity.getWindow();
        WindowManager.LayoutParams lp = activityWindow.getAttributes();
        lp.alpha = alpha;
        activityWindow.setAttributes(lp);

        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                WindowManager.LayoutParams lp = activityWindow.getAttributes();
                lp.alpha = 1;
                activityWindow.setAttributes(lp);

            }
        });
    }

    /**
     * 设置对话框底部弹出模式
     * 用法：new DialogUtil().setBottomMode(dialog);
     *
     * @param dialog AlertDialog
     */
    public void setBottomMode(AlertDialog dialog) {
        setBottomMode(dialog, null, -1);
    }

}